package net.sharpf2.api.interfaces;

import net.sharpf2.sfcore.client.Interface;
import net.sharpf2.api.Globals;
import net.sharpf2.api.game.Interfaces;
import net.sharpf2.api.types.Content;
import net.sharpf2.api.util.Mathematics;
import net.sharpf2.sfcore.input.Mouse;

/**
 * Class to handle the magic tab.
 * @author Piotr Gajdowski
 */
public class Magic {
    
    Mouse mouse = Globals.CONTROLLER.getMouse();
    
    /**
     * Return the actual interface object for the magic tab.
     * @return Content<Interface>
     */
    public static Content<Interface> getInterface() {
        int tab = magicsTab();
        return Interfaces.get(Constants.MAGICS_TABS[tab], 0);
    }
    
    /**
     * Returns the children of the magic tab.
     * @param   child Magic tab child ID.
     * @return  Content<Interface>
     */
    public static Content<Interface> getChild(int child) {
        int tab = magicsTab();
        return Interfaces.get(Constants.MAGICS_TABS[tab], child);
    }
    
    /**
     * Casts a magic spell.
     * @param id        use interface explorer to find the ID you need, between 24 and 91, and 98.
     * @param autocast  true if you want to click the action "Autocast" otherwise false.
     */
    public static void cast(int id, boolean autocast) {
        int tab = magicsTab();
        Tabs.checkAndOpen(Tabs.Constants.TAB_MAGIC);
        String action = autocast ? "(Autocast|Autozauber|Lancer automatiquement)" : "(Cast|Zaubern|Lancer)";
        Content<Interface> iface = Interfaces.get(Constants.MAGICS_TABS[tab], id);
        if (iface.getContent().getAbsoluteY() >= Constants.SCROLLABLE) {
            Content<Interface> scrollbar = Interfaces.get(Constants.MAGICS_TABS[tab], Constants.SCROLLBAR[tab]);
            Interface[] scrollchilds = scrollbar.getContent().getChildren();
            if (scrollchilds[1].getAbsoluteY() != Constants.SCROLLED[tab]) {
                int x = scrollchilds[1].getAbsoluteX()+Mathematics.random(2, 14);
                int y = scrollchilds[1].getAbsoluteX()+Mathematics.random(2, 14);
                Globals.CONTROLLER.getMouse().press(x, y, true);
                Globals.CONTROLLER.getMouse().move(x+Mathematics.random(51, 60), y+Mathematics.random(1, 3));
                Globals.CONTROLLER.getMouse().release(x+Mathematics.random(51, 60), y+Mathematics.random(1, 3), true);
            }
        }
        for (int i=0; i<iface.getContent().getActions().length; i++) {
            if (iface.getContent().getActions()[i].contains(action)) {
                iface.getActions().click(action);
            }
        }
    }
    
    /**
     * Clicks a specified sort button at the bottom of the magic tab to sort the magic spells.
     * @param orderID   Indicates which button to click, use the Constants class to select the button.
     * @param onoff     true to set it on, otherwise false.
     */
    public static void sort(int orderID, boolean onoff) {
        int tab = magicsTab();
        Tabs.checkAndOpen(Tabs.Constants.TAB_MAGIC);
            Content<Interface> orderButton = Interfaces.get(Constants.MAGICS_TABS[tab], orderID);
            if (onoff && orderButton.getContent().getTextureId() != Constants.TEXTURE_ID_ON) {
                orderButton.getActions().click();
            }
            if (!onoff && orderButton.getContent().getTextureId() == Constants.TEXTURE_ID_ON) {
                orderButton.getActions().click();
            }
    }
    
    /**
     * Clicks a specified hide button at the bottom of the magic tab to hide spells.
     * @param button    the name of the button to indicate which one to click, use the Constants class to select the button.
     * @param hide      true to hide, false to show.
     */
    public static void hide(int button, boolean hide) {
        int tab = magicsTab();
        Tabs.checkAndOpen(Tabs.Constants.TAB_MAGIC);
            Content<Interface> hideButton = Interfaces.get(Constants.MAGICS_TABS[tab], button);
            if (hide && hideButton.getContent().getTextureId() != Constants.TEXTURE_ID_ON) {
                hideButton.getActions().click();
            }
            if (!hide && hideButton.getContent().getTextureId() == Constants.TEXTURE_ID_ON) {
                hideButton.getActions().click();
            }
    }
    
    /**
     * Toggle Defensive casting on/off.
     * @param onoff true to enable, false to disable.
     */
    public static void toggleDefCasting(boolean onoff) {
        int tab = magicsTab();
        Tabs.checkAndOpen(Tabs.Constants.TAB_MAGIC);
            Content<Interface> toggleButton = Interfaces.get(Constants.MAGICS_TABS[tab], Constants.DEFENSIVE_CASTING[tab]);
            if (toggleButton != null) {
                if (onoff && toggleButton.getContent().getTextureId() != Constants.TEXTURE_ID_ON) {
                    toggleButton.getActions().click();
                }
                if (!onoff && toggleButton.getContent().getTextureId() == Constants.TEXTURE_ID_ON) {
                    toggleButton.getActions().click();
                }
            }
    }
    
    /**
     * Checks if Defensive Casting is on.
     * @return true if yes, otherwise false.
     */
    public static boolean isDefCastingOn() {
        if (Globals.CLIENT.getSettings().getData()[Constants.SETTING_DEFENSIVE_CASTING_ON] == Constants.SETTING_DEFENSIVE_CASTING_ON_VALUE) {
            return true;
        }
        return false;
    }
    
    /**
     * Checks if magic tab is sorted by given ID button.
     * @param   id    Use Constants class to indicate for which sort button to check for (SETTING_SORTED_*****).
     * @return  true if is sorted, otherwise false.
     */
    public static boolean isSorted(int id) {
        if (Globals.CLIENT.getSettings().getData()[Constants.SETTING_SORTED] == id) {
            return true;
        }
        return false;
    }
    
    /**
     * Checks if Autocasting is on.
     * @return true if character is autocasting, otherwise false.
     */
    public static boolean isAutocasting() {
        if (Globals.CLIENT.getSettings().getData()[Constants.SETTING_AUTOCASTING] > 0) {
            return true;
        }
        return false;
    }
    
    /**
     * Checks which magic tab is available
     * @return Array index of MAGICS_TABS
     */
    public static int magicsTab() {
        for (int i=0; i<Constants.MAGICS_TABS.length; i++) {
            boolean toggleButton = Interfaces.isInterfaceValid(i);
            if (toggleButton) {
                return i;
            }
        }
        return -1;
    }
    
    public static class Constants {
        public final static int[] MAGICS_TABS = {
                                                    192, // normal magics
                                                    430 // lunar magics
                                                };
        public final static int SETTING_DEFENSIVE_CASTING_ON = 439;
        public final static int SETTING_DEFENSIVE_CASTING_ON_VALUE = 256;
        public final static int SETTING_SORTED = 1376;
        public final static int SETTING_SORTED_LEVEL = 0;
        public final static int SETTING_SORTED_COMBAT = 1;
        public final static int SETTING_SORTED_TELEPORTS = 2;
        public final static int SETTING_AUTOCASTING = 108;
        public final static int[] DEFENSIVE_CASTING = {2,20};
        public final static int[] HIDE_COMBAT = {7,5};
        public final static int[] HIDE_TELEPORT = {9,7};
        public final static int[] HIDE_MISCELLANEOUS = {11,9};
        public final static int[] HIDE_SKILL = {13,11};
        public final static int[] SORT_LEVEL = {15,11};
        public final static int[] SORT_COMBAT = {16,12};
        public final static int[] SORT_TELEPORTS = {17,13};
        public final static int TEXTURE_ID_ON = 1703;
        public final static int[] SCROLLBAR = {94,62};
        public final static int SCROLLABLE = 410;
        public final static int[] SCROLLED = {275};
    }
    
}
